var config = require('../../../utils/config');
var API_URL = config.API_URL;
var util = require('../../../utils/util');
var app = getApp();
var page, loading, userInfo;
let Pay = util.wxPromise(my.tradePay);
let getOrder = that => {
    if (loading) {
        return
    }
    loading = true;
    let params = {
        //   传递参数 页数 卖家买家订单 订单状态
        page: page,
        status: that.data.orderStatus,
        type: that.data.orderType
    }
    util.PRL(API_URL.order_list, params)
        .then(res => {
            console.log(res)
            let loadingText;
            let orders;
            if (page == 1) {
                orders = new Array();
            } else {
                orders = that.data.orders;
            }
            orders = orders.concat(res.data.data.orders);
            if (res.data.hasMore == 0) {
                loadingText = '没有更多了';
            } else {
                loadingText = '加载中...';
            }
            if (res.data.data.orders.length != 0) {
                page++
            }
            that.setData({
                orders: orders,
                loadingText: loadingText,
                template: 'order'
            })
            loading = false;
            my.hideLoading();
            my.stopPullDownRefresh();
        }).catch(res => {
            my.hideLoading();
            my.stopPullDownRefresh();
        })
}
let getList = function (that) {
    if (loading) {
        return
    }
    loading = true;
    let params = {
        page: page,
        from: that.data.orderType
    }
    // 拿到售后信息
    util.PRL(API_URL.refund_list, params)
        .then(res => {
            let list, data;
            data = res.data.data.orders;
            if (page == 1) {
                list = new Array();
            } else {
                list = that.data.list;
            }
            list = list.concat(data);
            if (data.length != 0) {
                page++
            }
            that.setData({
                list: list,
                template: 'refund'
            })
            loading = false;
            my.hideLoading();
            my.stopPullDownRefresh();
        }).catch(res => {
            loading = false;
            my.hideLoading();
            my.stopPullDownRefresh();
        })
}

Page({
    data: {
        template:"order",
        hidden: true,
        orderStatus: 0,
        orderType: 'buyer_order',
        tag: [
            { name: '买家', class: "select", type: 'buyer_order' },
            { name: '卖家', class: "no_select", type: 'seller_order' }
        ],
        state: [
            { name: "全部", class: "select", id: 0 },
            { name: "待付款", class: "no_select", id: 11 },
            { name: "待发货", class: "no_select", id: 20 },
            { name: "待收货", class: "no_select", id: 30 },
            { name: "已完成", class: "no_select", id: 40 },
        ],
        hidden_e: true,
        hidden_c: true,
        express: [{
            company: 'zhaijisong',
            name: '宅急送',
            checked: true
        }, {
            company: 'yunda',
            name: '韵达快运',
            checked: false
        }, {
            company: 'zhongtong',
            name: '中通速递',
            checked: false
        }, {
            company: 'yuantong',
            name: '圆通速递',
            checked: false
        }, {
            company: 'shunfeng',
            name: '顺丰速运',
            checked: false
        }, {
            company: 'shentong',
            name: '申通快递',
            checked: false
        }, {
            company: 'tiantian',
            name: '天天快递',
            checked: false
        }, {
            company: 'huitongkuaidi',
            name: '汇通快递',
            checked: false
        }, {
            company: 'ems',
            name: 'EMS',
            checked: false
        }],
        express_index: 0
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        // 判断是那种类型
        console.log(options)
        var that = this;
        loading = false;
        app.globalData.goToOrder = false;
        var id = options.id;
        var state = that.data.state;
        var index;
        switch (id) {
            case '0': index = 0; break;
            case '11': index = 1; break;
            case '20': index = 2; break;
            case '30': index = 3; break;
            case '40': index = 4; break;
        }
        // 这里把所有列表项 都加上未选中
        state = state.map(item => {
            item.class = 'no_select';
            return item;
        })
        // 拿到当前订单的Index
        state[index].class = 'select';
        that.setData({
            state: state,
            orderStatus: state[index].id
        })
        page = 1;
        userInfo = my.getStorageSync({ key: 'user' }).data;
        getOrder(that);
        let hasIdentified = my.getStorageSync({ key: 'hasIdentified' }).data;
        this.setData({
            hasIdentified: hasIdentified
        })
        if (state[index].id != -1) {
            getOrder(that);
        } else {
            getList(that);
        }

    },
    changeType: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var tag = that.data.tag;
        var orderType = tag[index].type;
        page = 1;
        tag = tag.map(item => {
            item.class = 'no_select';
            return item;
        })
        tag[index].class = 'select';
        that.setData({
            tag: tag,
            orderType: orderType
        })
        getOrder(that);
        /* if (that.data.orderType == 'buyer_order') {
          getBuyerOrder(that);
        } else {
          getSellerOrder(that);
        } */
    },
    // 切换订单状态
    orderStatusChange: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        var orderStatus = that.data.orderStatus;
        var state = that.data.state;
        if (orderStatus != state[index].id) {
            page = 1;
            state = state.map(item => {
                item.class = 'no_select';
                return item;
            })
            state[index].class = 'select';
            that.setData({
                state: state,
                orderStatus: state[index].id
            })
            getOrder(that);
        }
    },
})